package com.yicong.ants.bean.base;

import android.text.TextUtils;

import com.cchao.simplelib.core.Logs;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

/**
 * @author cchao
 * @version 2019-04-29.
 */
public class BaseBean {

    public String code;
    private String json;
    public String msg;
    private String retcode;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public boolean isCodeSuc() {
        return "0".equals(code) ||"00".equals(code) || "2000".equals(code) || "2000".equals(retcode);
    }

    public boolean isCodeGlobalTip() {
        return "4000".equals(code) || "4010".equals(code);
    }

    public boolean isCodeFail() {
        return !isCodeSuc() || "20".equals(code);
    }

    public boolean isCodeEmpty() {
        return code.equals("20");
    }

    public String getMsg() {
        if (isCodeGlobalTip()) {
            return "";
        }

        if (!TextUtils.isEmpty(msg)) {
            return msg;
        } else if (!TextUtils.isEmpty(getJson())) {
            try {
                JsonElement element = new JsonParser().parse(getJson())
                    .getAsJsonObject()
                    .getAsJsonObject("result")
                    .get("msg");

                if (element != null) {
                    msg = element.getAsString();
                    return msg;
                }

            } catch (Exception e) {
                Logs.logException(e);
            }
        }
        return "";
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getJson() {
        return json;
    }

    public void setJson(String json) {
        this.json = json;
    }

}
